package org.ucas.kgraph.dto;

import org.springframework.http.HttpStatus;
import org.ucas.kgraph.dto.GraphDto.Node.Link;
import org.ucas.kgraph.entity.RelationType;
import org.ucas.kgraph.entity.Term;
import org.ucas.kgraph.entity.TermGraph;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class TermGraphDto extends ResponseDto {

  public List<Node> data;

  public TermGraphDto(){
    super(HttpStatus.OK,null);
    data = new ArrayList<>();
  }

  public static TermGraphDto of(TermGraph graph){
    TermGraphDto dto = new TermGraphDto();
    Map<Long,Node> nodes = new HashMap<>();
    graph.terms.stream().forEach(term -> {
      Node node = Node.of(term);
      dto.data.add(node);
      nodes.put(term.getId(),node);
    });
    nodes.get(graph.rootId).isroot = true;

    graph.relations.stream().forEach(relation -> {
      if(relation.getCategory().equals(RelationType.Parent)){
        nodes.get(relation.getLeftEnd()).parentid = nodes.get(relation.getRightEnd()).id;
      }else{
    	Link tmplink=new Link();
    	
    	tmplink.setTo(relation.getLeftEnd().toString());
    	String[] tempContent = relation.getName().split(";");
    	tmplink.setLinkmsg(tempContent[0]);
    	tmplink.setRefer1x(Float.parseFloat(tempContent[1]));
    	tmplink.setRefer1y(Float.parseFloat(tempContent[2]));
    	tmplink.setRefer2x(Float.parseFloat(tempContent[3]));
    	tmplink.setRefer2y(Float.parseFloat(tempContent[4]));
    	nodes.get(relation.getRightEnd()).getLink().add(nodes.get(relation.getRightEnd()).getLink().size(), tmplink);
      }
    });
    return dto;
  }

  public static class Node{
    public String id;
    public String parentid;
    public String topic;
    public String tags;
    public Long document;
    public boolean isroot;
    public List<Link> link=new ArrayList<>();
    public String direction;
    public Float cost;
    
    public static Node of(Term term){
      Node node = new Node();
      node.id = term.getId().toString();
      node.topic = term.getDefinition();
      node.isroot = false;
      node.tags = term.getTags();
      node.document=term.getDocumentId();
      node.direction=term.getDirection();
      node.cost=term.getCost();
      return node;
    }
    
    
    public String getTags() {
    	return tags;
    }
    public void setTags(String tags) {
    	this.tags = tags;
    }
    public String getId() {
      return id;
    }

    public void setId(Long id) {
      this.id = id.toString();
    }

    public String getParentid() {
      return parentid;
    }

    public void setParentid(Long parentid) {
      this.parentid = parentid.toString();
    }

    public String getTopic() {
      return topic;
    }

    public void setTopic(String topic) {
      this.topic = topic;
    }

    public boolean isroot() {
      return isroot;
    }

    public void setIsroot(boolean isroot) {
      this.isroot = isroot;
    }

	public Long getDocument() {
		return document;
	}

	public void setDocument(Long document) {
		this.document = document;
	}
	public List<Link> getLink() {
		return link;
	}
	public void setLink(List<Link> link) {
		this.link = link;
	}


	public String getDirection() {
		return direction;
	}


	public void setDirection(String direction) {
		this.direction = direction;
	}


	public Float getCost() {
		return cost;
	}


	public void setCost(Float cost) {
		this.cost = cost;
	}
	
    
  }

  public List<Node> getData() {
    return data;
  }

  public void setData(List<Node> data) {
    this.data = data;
  }
}
